package com.beta.admin.controller;
import com.baomidou.mybatisplus.plugins.Page;
import com.beta.common.web.ContentType;
import com.beta.admin.request.Version.VersionInfoRequest;
import com.beta.admin.support.web.BaseController;
import com.beta.admin.vo.VersionInfoVo;
import com.beta.dto.version.VersionInfoDto;
import com.beta.entity.VersionInfos;
import com.beta.service.IVersionInfosService;
import ma.glasnost.orika.MapperFacade;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by lichao on 2017/1/11.
 */
@RestController
@RequestMapping(value = RequestMappingConstants.MAPPING_PREFIX + "/version_infos")
public class VersionInfosController extends BaseController {
    @Autowired
    private MapperFacade beenMapper;
    @Autowired
    private IVersionInfosService versionInfosService;
    @RequiresPermissions("version_infos:list")
    @GetMapping(value = "", produces = ContentType.JSON)
    public Object getVersionInfos(){
        Page<VersionInfos> versionInfosPage = versionInfosService.getVersionInfos(getMybatisPage());
        return pageData(versionInfosPage, versionInfos -> beenMapper.map(versionInfos, VersionInfoVo.class));
    }
    @RequiresPermissions("version_infos:view")
    @GetMapping(value = "/{id}", produces = ContentType.JSON)
    public Object getVersionInfo(@PathVariable("id")Integer id){
        VersionInfos versionInfo = versionInfosService.getVersionInfo(id);
        VersionInfoVo map = beenMapper.map(versionInfo, VersionInfoVo.class);
        return ok(map);
    }
    @RequiresPermissions("version_infos:edit")
    @PatchMapping(value = "/{id}",produces = ContentType.JSON)
    public Object updateVersionInfo(@PathVariable("id")Integer id,
                                    @Valid @RequestBody VersionInfoRequest versionRequest){
        VersionInfoDto versionDto=new VersionInfoDto();
        versionDto.setVersion(versionRequest.getVersion());
        versionDto.setContent(versionRequest.getContent());
        versionDto.setForceUpdate(versionRequest.getForceUpdate());
        versionDto.setAndroidUrl(versionRequest.getAndroidUrl());
        versionDto.setIosUrl(versionRequest.getAndroidUrl());
        versionInfosService.updateVersionInfo(id,versionDto);
        return null;
    }

    @RequiresPermissions("version_infos:new")
    @PostMapping(value = "",produces = ContentType.JSON)
    public void createVersionInfo(@Valid @RequestBody VersionInfoRequest versionRequest){
        VersionInfoDto versionDto=new VersionInfoDto();
        versionDto.setVersion(versionRequest.getVersion());
        versionDto.setContent(versionRequest.getContent());
        versionDto.setForceUpdate(versionRequest.getForceUpdate());
        versionDto.setAndroidUrl(versionRequest.getAndroidUrl());
        versionDto.setIosUrl(versionRequest.getAndroidUrl());
        versionInfosService.createVersionInfo(versionDto);
    }

    @RequiresPermissions("version_infos:edit")
    @DeleteMapping(value = "/{id}",produces = ContentType.JSON)
    public void deleteVersionInfo(@PathVariable("id")Integer id){
        versionInfosService.deleteVersionInfo(id);
    }
}
